package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.BindDao;
import io.aircraft.modules.wms.dto.BindDTO;
import io.aircraft.modules.wms.entity.BindEntity;
import io.aircraft.modules.wms.entity.stored.ProcBindEntity;
import io.aircraft.modules.wms.entity.stored.ProcManualRegisterEntity;
import io.aircraft.modules.wms.service.BindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 绑盘信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-18
 */
@Service
public class BindServiceImpl extends CrudServiceImpl<BindDao, BindEntity, BindDTO> implements BindService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<BindDTO> page(Map<String, Object> params) {
        IPage<BindEntity> page = baseDao.selectPage(getPage(params, "bind_id", false),
                getWrapper(params));
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, BindDTO.class);
    }

    @Override
    public QueryWrapper<BindEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public ProcResult bind(Map<String, Object> params) {
        ProcBindEntity parameter = new ProcBindEntity();
        String contNo = (String) params.get("contNo");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        parameter.setContNo(contNo);
        parameter.setSpuNo(spuNo);
        parameter.setLotNo(lotNo);
        //逻辑处理(托盘与订单绑定并生成设备任务)
        baseDao.procEmptyLpnRegister(parameter);
        return new ProcResult().result(parameter.getCode(), parameter.getMsg());
    }

    @Override
    public ProcResult manualRegister(Map<String, Object> params) {
        ProcManualRegisterEntity parameter = new ProcManualRegisterEntity();
        String contNo = (String) params.get("contNo");
        String spuNo = (String) params.get("spuNo");
        parameter.setContNo(contNo);
        parameter.setSpuNo(spuNo);
        baseDao.procManualRegister(parameter);
        return new ProcResult().result(parameter.getCode(), parameter.getMsg());
    }
}